/**
 * <p>Title: LoginRestImpl</p>
 * <p>Description: The Implements of LoginRest</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author qianpengji
 * @version 1.0
 * <pre>Histroy:
 *       2016-11-30  qianpengji  Create
 *</pre>
*/
package cn.jhg.account.rest;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import cn.jhg.account.entity.User;
//import cn.jhg.account.service.batchOrder.BuildPreviewOrderImpl;
import cn.jhg.account.service.LoginSvc;
import cn.jhg.account.util.IpAddrUtil;
import cn.jhg.account.util.JsonUtil;
import cn.jhg.account.util.ServiceResult;


@Component
//@Lazy
public class LoginRestImpl implements LoginRest {

	@Resource
	private LoginSvc loginSvc;
//	@Autowired
//	private HttpSession session;
	private Logger logger = LoggerFactory.getLogger(LoginRestImpl.class);
	/**
     * 用户登录
     * @param account	用户账号
     * @param password  用户密码
     * @return			验证码/erroCode
     */
	
	@Override
	public String login(String account, String password,HttpServletRequest request) {
		
		String ip=IpAddrUtil.getIpAddr(request);
		System.out.println(ip);
		
		request.getSession().setAttribute("ipAddr",ip);
		ServiceResult<User> serviceResult=loginSvc.login(account, password);
		int errorCode=serviceResult.getErrorCode();
		User user=serviceResult.getData();
		if(errorCode==0){
			request.getSession().setAttribute("userId",user.getId());
			System.out.println(request.getSession().getAttribute("userId"));
		}
		return JsonUtil.packaging(serviceResult);
	}

	/**
     * 用户登出
     * @param uid	            用户id
     * @return			erroCode
     */
	@Override
	public ServiceResult<Object> logout(int uid) {
//		session.invalidate();
//		System.out.println("已销毁session"+session.getId());
		ServiceResult<Object> serviceResult=loginSvc.logout(uid,1);
		return serviceResult;
	}

	/**
     * 用户修改密码
     * @param account	用户账号
     * @param oldPwd	用户旧密码
     * @param newPwd	用户新密码
     * @return			erroCode
     */
	@Override
	public ServiceResult<Object> changePassword(String account, String oldPwd, String newPwd) {
		// TODO Auto-generated method stub
		ServiceResult<Object> serviceResult=loginSvc.changePassword(account, oldPwd, newPwd);
		return serviceResult;
	}

	/**
     * 重置密码
     * @param uid	            用户id
     * @return			erroCode/新密码
     */
	@Override
	public ServiceResult<String> resetPassword(int uid) {
		// TODO Auto-generated method stub
		ServiceResult<String> serviceResult=loginSvc.resetPassword(uid, 1, 0);
		return serviceResult;
	}

	/**
     * 获取密码有效期
     * @param uid	            用户id
     * @return			erroCode/密码有效期
     */
	@Override
	public ServiceResult<Timestamp> getPwdExpiryDate(int uid) {
		// TODO Auto-generated method stub
		ServiceResult<Timestamp> serviceResult=loginSvc.getPwdExpiryDate(uid);
		return serviceResult;
	}

}
